package com.hongkzh.www.adv.service;

import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.ArrayList;
import java.util.List;

/**
 * @author martins
 */
public class AdvService extends BaseService {
    private static String imgSrc;


    public List<Record> getRecommendList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getRecommendList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);

        }
        return records;

    }

    public List<Record> getStaticList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getStaticList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);

        }
        return records;

    }

    public List<Record> getCityAdvList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getCityAdvList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }

        SqlPara categorySql = Db.getSqlPara("adv.getCityTagList");
        List<Record> categorys = Db.find(categorySql);
        if (categorys == null) {
            categorys = new ArrayList<>();
        }

        String[] tags;

        for (Record r : records) {
            r.set("tags", categorys);
            List<Record> categorysd = new ArrayList<>();
            if (StrKit.notBlank(r.getStr("tag"))) {
                tags = r.getStr("tag").split(",");
                for (String tag : tags
                        ) {
                    for (Record c : categorys
                            ) {
                        if (tag.equals(c.getStr("did"))) {
                            categorysd.add(c);
                            break;
                        }
                    }

                }
            }
            if (categorysd.size() > 0) {
                r.set("tags", categorysd);
            }
            doFilePath(r);

        }

        return records;

    }

    public List<Record> getCityList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getCityList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;

    }

    public List<Record> getMediaList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getMediaList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;

    }

    public List<Record> getEnterpriseList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getEnterpriseList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {

            doFilePath(r);
        }
        return records;

    }

    public List<Record> getTraditionList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getTraditionList");
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;

    }

    public Page<Record> getHotAdvList(int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("adv.getHotAdvList");
        Page<Record> records = Db.paginate(pageNumber, StaticPublic.PAGESIZE, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);
        }
        return records;

    }

    public Record getAdvById(String id) {
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvById", id);
        Record r = Db.findFirst(sqlPara);
        if (r != null) {
            doFilePath(r);
            String type=r.getStr("type");
            SqlPara categorySql=null;

            if("1".equals(type)){
                categorySql = Db.getSqlPara("adv.getMediaTagList");
            }
            else if("2".equals(type)){
                categorySql = Db.getSqlPara("adv.getEnterpriseTagList");
            }
            else if("3".equals(type)){
                categorySql = Db.getSqlPara("adv.getCityTagList");
            }
            else  if("4".equals(type)){
                categorySql = Db.getSqlPara("adv.getTraditionTagList");
            }
            List<Record> categorys = Db.find(categorySql);
            if (categorys == null) {
                categorys = new ArrayList<>();
            }
            r.set("tags", categorys);
            String[] tags;
            List<Record> categorysd = new ArrayList<>();
            if (StrKit.notBlank(r.getStr("tag"))) {
                tags = r.getStr("tag").split(",");
                for (String tag : tags
                        ) {
                    for (Record c : categorys
                            ) {
                        if (tag.equals(c.getStr("did"))) {
                            categorysd.add(c);
                            break;
                        }
                    }

                }
            }
            if (categorysd.size() > 0) {
                r.set("tags", categorysd);
            }
        }

        return r;

    }


    public Page<Record> getAdvCommentsById(String id, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvCommentsById", id);
        Page<Record> records = Db.paginate(pageNumber, 10, sqlPara);

        for (Record r : records.getList()) {
            doFilePath(r);
        }
        return records;

    }


    public List<Record> getEnterpriseHotAdvTypeList() {
        SqlPara sqlPara = Db.getSqlPara("adv.getEnterpriseHotAdvTypeList");
        return Db.find(sqlPara);
    }

    public List<Record> getEnterpriseHotAdvList(String id) {
        SqlPara sqlPara = Db.getSqlPara("adv.getEnterpriseHotAdvList", id);
        List<Record> records = Db.find(sqlPara);
        if (records == null) {
            records = new ArrayList<>();
        }
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }

    public List<Record> getEnterpriseCategoryList() {
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseCategoryList");
        List<Record> records = Db.find(sqlPara);
        return records;
    }

    public Page<Record> getEnterpriseCategoryAdvList(String cid, int pageNumber, String sortId, String sortValue) {
        if("create_date".equals(sortId)) {sortId=" a.create_date ";}
        else if("rank".equals(sortId)){ sortId=" a.img_rank ";}
        Kv cond=Kv.by("cid",cid).set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("enterprise.getEnterpriseCategoryAdvList",cond);
        Page<Record> records=Db.paginate(pageNumber,3,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    public List<Record> getAdvRecommendByAdvId(String id) {
        SqlPara sqlPara = Db.getSqlPara("adv.getAdvRecommendByAdvId",id);
        List<Record> records = Db.find(sqlPara);
        for (Record r : records) {
            doFilePath(r);
        }
        return records;
    }
}
